/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.properties;

import com.funcom.peeler.WorkBench;
import com.funcom.peeler.peelable.PeelButton;
import com.funcom.peeler.peelable.PeelComponent;
import com.funcom.peeler.peelable.PeelToggleButton;

import javax.swing.*;
import java.util.Set;

public class StateBoxes extends UpdateableProperties {

    JCheckBox[] stateBoxes;

    public static final int DEFAULT = 0;
    public static final int HOVER = 1;
    public static final int DISABLE = 2;
    public static final int DOWN = 3;
    public static final int SELECTED = 4;
    public static final int DISSELECTED = 5;

    public static final int LENGTH = 6;

    public StateBoxes(JCheckBox[] stateBoxes) {
        this.stateBoxes = stateBoxes;
        
        stateBoxes[DEFAULT].putClientProperty( "state", null );
        stateBoxes[HOVER].putClientProperty( "state", "hover" );
        stateBoxes[DISABLE].putClientProperty( "state", "disable" );
        stateBoxes[DOWN].putClientProperty( "state", "down" );
        stateBoxes[SELECTED].putClientProperty( "state", "selected" );
        stateBoxes[DISSELECTED].putClientProperty( "state", "disselected" );

        initListeners();
    }

    public void update() {
        fields_cleared = false;
        setStateBoxes();
    }

    public void needUpdate( WorkBench workBench,
                            Set<PeelComponent> allComp,
                            Set<PeelComponent> selectedComps,
                            PeelComponent main_selected,
                            PropertiesFrame thisFrame ) {

        if (main_selected == null) {
            selectedComp = null;
            if (!fields_cleared) {
                wantUpdate = true;
                return;
            } else {
                return;
            }
        }

        if (selectedComp != main_selected) {
            wantUpdate = true;
            selectedComp = main_selected;
        }
    }

    public void applyChange( PeelComponent component, PropertiesFrame updater ) { }

    public void clear() {
        disableCheckBoxes();
    }

    private void setStateBoxes() {

        if (selectedComp == null) {
            disableCheckBoxes();
            return;

        } else {
            stateBoxes[DEFAULT].setEnabled(true);
            stateBoxes[HOVER].setEnabled(true);
            stateBoxes[DISABLE].setEnabled(true);

            if (!stateBoxes[DEFAULT].isSelected()) {
                stateBoxes[DEFAULT].doClick();
            }
        }

        if (selectedComp instanceof PeelButton) {
            stateBoxes[DOWN].setEnabled(true);
        } else {
            if (stateBoxes[DOWN].isSelected()) {
                stateBoxes[DOWN].doClick();
            }
            stateBoxes[DOWN].setEnabled(false);
        }

        if (selectedComp instanceof PeelToggleButton) {
            stateBoxes[DISSELECTED].setEnabled(true);
            stateBoxes[SELECTED].setEnabled(true);
        } else {
            if (stateBoxes[DISSELECTED].isSelected()) {
                stateBoxes[DISSELECTED].doClick();
            }
            if (stateBoxes[SELECTED].isSelected()) {
                stateBoxes[SELECTED].doClick();
            }
            stateBoxes[DISSELECTED].setEnabled(false);
            stateBoxes[SELECTED].setEnabled(false);
        }

        fields_cleared = false;
    }

    private void disableCheckBoxes() {
        for (JCheckBox aState : stateBoxes) {
            aState.setSelected( false );
            aState.setEnabled(false);
        }
        fields_cleared = true;
    }

    protected void initListeners() {

    }
}
